package com.xxd.gulimail.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xxd.gulimail.product.entity.AttrEntity;
import com.xxd.gulimail.product.service.AttrService;
import com.xxd.gulimail.product.service.CategoryService;
import com.xxd.gulimail.product.vo.AttrGroupRelationVo;
import com.xxd.gulimail.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xxd.gulimail.product.entity.AttrGroupEntity;
import com.xxd.gulimail.product.service.AttrGroupService;
import com.xxd.common.utils.PageUtils;
import com.xxd.common.utils.R;


/**
 * 属性分组
 *
 * @author xxd
 * @email 2132862687@qq.com
 * @date 2022-09-26 10:32:57
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    AttrService attrService;
/**
 * @Description: 查出分类下的所有属性分组，以及属性
 * @Author: xuxd
 * @param
 * @Date: 2022/10/17 23:48
 */
//    product/attrgroup/225/withattr
@GetMapping("/{categoryId}/withattr")
public R getAttrGroupWithAttr(@PathVariable("categoryId") long categoryId) {
    List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrByCatId(categoryId);
    return R.ok().put("data",vos);
}
    // /product/attrgroup/attr/relation
    /**
     * @Description: 新增关联
     * @Author: xuxd
     * @Date: 2022/10/17 19:54
     */
    @PostMapping("/attr/relation")
    public R saveRelation(@RequestBody AttrGroupRelationVo[] vos) {
        attrService.saveRelation(vos);

        return R.ok();
    }

    // "/1/attr/relation"
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") long attrgroupId) {
        List<AttrEntity> entities= attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entities);
    }
    //  /product/attrgroup/1/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R noattrRelation(@RequestParam Map<String, Object> params,@PathVariable("attrgroupId") long attrgroupId) {
        PageUtils page= attrService.getNoAttrRelationAttr(params,attrgroupId);
        return R.ok().put("data",page);
    }

    //  product/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {
        attrService.deleteRelation(vos);

        return R.ok();
    }
        /**
         * 列表
         */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") long catelogId) {
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] path = categoryService.getcatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
